var RealTrendChart = function (params) {
    var self = this;
    self.chartId = params.chartId;
    self.chartQuery = params.chartQuery;
    self.option = null;
    self.chart = null;
    self.points = null;
    self.ajaxRequests = null;
    self.INTERVAL = 0;
    self.getInterval = params.getInterval;
    self.lineDataArray = new Array();
    self.seriesArray = new Array();
    self.overflows = new Array();
    self.isInited = false; //根据属性设置最大最小值
    self.render = function () {
        self.option.series = self.seriesArray;
        self.chart.setOption(self.option);
    };
    self.begin = function () {
        try {
            if (self.ajaxRequests) {
                $.each(self.ajaxRequests, function (i, d) {
                    if (d) {
                        d.abort();
                    }
                });
            }
        }
        catch (ex) {
            console.log(ex);
        }

        try {
            self.INTERVAL = self.getInterval() * 60 * 1000;
            self.points = self.chartQuery.points;
            self.reset(self.points);

            self.ajaxRequests = new Array();
            self.overflows = new Array();
            $.each(self.points, function (i, d) {
                self.request(i, d);
            });
        }
        catch
            (e) {
            console.log(e.message);
        }
    };
    self.dataReceived = function (point, data) {
        debugger;
        var lineData = self.getLineData(point.deviceCode, point.pointCode);
        lineData.list.push(data);
        var max, min;
        $.each(self.lineDataArray, function (i, d) {
            if (d.list.length > 1) {
                var dMax = self.string2date(d.list[d.list.length - 1].value[0]);
                if (!max)
                    max = dMax;
                else
                    max = max >= dMax ? max : dMax;

                var dMin = self.string2date(d.list[0].value[0]);
                if (!min)
                    min = dMin;
                else
                    min = min <= dMin ? min : dMin;

                var isOverflow = (max.getTime() - min.getTime()) > self.INTERVAL;
                if (isOverflow) {
                    self.overflows[i] += 1;
                }
                if (self.overflows[i] > 10) {
                    d.list.shift();
                }
            }
        });

        self.option.xAxis.max = "dataMax";
        self.option.xAxis.min = new Date(max.getTime() - self.INTERVAL).format("yyyy-MM-dd hh:mm:ss");
        self.render();
    };
    self.request = function (i, d) {
        var ap = ajaxPost(contextRoot + "realtime/getEigenValue",
            d,
            function (data) {
                try {
                    self.dataReceived(d, data);
                }
                catch (e) {
                    console.log(e);
                }
                self.request(i, d);
            },
            function (error, xhr, status) {
                if (status != "abort") {
                    self.request(i, d);
                }
            }, 60000
        );
        self.ajaxRequests[i] = ap;
    };
    self.init = function () {
        self.chart = echarts.init(document.getElementById(self.chartId));
        self.option = {
            title: {
                left: 'left',
                text: '趋势图'
            },
            tooltip: {
                trigger: 'axis',
                formatter: function (params) {
                    params = params[0];
                    var date = new Date(params.value[0]);
                    return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1];
                },
                axisPointer: {
                    type: 'cross',
                    animation: false
                }
            },
            legend: {
                tooltip: {
                    show: true
                },
                data: []
            },
            toolbox: {
                feature: {
                    saveAsImage: {show: true}
                }
            },
            dataZoom: [
                {
                    type: 'slider',
                    show: true,
                    xAxisIndex: [0],
                    start: 0,
                    end: 100
                },
                {
                    type: 'slider',
                    show: true,
                    yAxisIndex: [0],
                    left: '93%',
                    start: 0,
                    end: 100
                },
                {
                    type: 'inside',
                    xAxisIndex: [0],
                    start: 0,
                    end: 100
                },
                {
                    type: 'inside',
                    yAxisIndex: [0],
                    start: 0,
                    end: 100
                }
            ],
            xAxis: {
                type: 'time',
                splitLine:
                    {
                        lineStyle: {
                            color: ['#aaa', '#ddd']
                        }
                    }
            }
            ,
            yAxis: {
                type: 'value',
                max: function (value) {
                    return Math.ceil(value.max + (value.max - value.min) / 10);
                },
                min: function (value) {
                    return Math.floor(value.min - (value.max - value.min) / 10);
                },
                splitLine: {
                    lineStyle: {
                        color: ['#aaa', '#ddd']
                    }
                }
                ,
                axisLine: {
                    show: true
                }
            }
            ,
            series: []
        };
        self.render();
    };
    self.getMin =
        self.reset = function (points) {
            self.points = points;
            self.lineDataArray = new Array();
            self.seriesArray = new Array();
            self.option.legend.data = new Array();
            $.each(points, function (i, d) {
                var lineData = self.createLineData(d.deviceCode, d.pointCode);
                self.lineDataArray.push(lineData);
                var series = self.createSeries(d.pointName, d.deviceCode, d.pointCode, lineData, d.color);
                self.seriesArray.push(series);
                self.option.legend.data.push(d.pointCode);
                self.overflows[i] = 0;
            });
        };
    self.getLineData = function (deviceCode, pointCode) {
        var lineData = null;
        $.each(self.lineDataArray, function (i, d) {
            if (d.deviceCode == deviceCode && d.pointCode == pointCode) {
                lineData = d;
                return true;
            }
        });

        return lineData;
    };
    self.replaceLineData = function (deviceCode, pointCode, data) {
        $.each(self.lineDataArray, function (i, d) {
            if (d.deviceCode == deviceCode && d.pointCode == pointCode) {
                self.lineDataArray.splice(i, 1, data);
                return true;
            }
        });
    };
    self.createLineData = function (deviceCode, pointCode, defaultData) {
        var lineData = new Object();
        lineData.list = new Array();
        if (defaultData) {
            lineData.list = defaultData;
        }
        lineData.deviceCode = deviceCode;
        lineData.pointCode = pointCode;
        return lineData;
    };
    self.getSeries = function (deviceCode, pointCode) {
        var series = null;
        $.each(self.seriesArray, function (i, d) {
            if (d.deviceCode == deviceCode && d.pointCode == pointCode) {
                series = d;
                return true;
            }
        });
        return series;
    };
    self.replaceSeriesData = function (deviceCode, pointCode, data) {
        var series = self.getSeries(deviceCode, pointCode);
        if (series) {
            series.data = data;
        }
    }
    self.createSeries = function (pointName, deviceCode, pointCode, data, color) {
        var series = {
            name: pointName,
            type: 'line',
            showSymbol: true,
            hoverAnimation: false,
            data: data.list,
            smooth: true,
            itemStyle: {
                normal: {
                    color: color,
                }
            },
            lineStyle: {
                normal: {
                    width: 1,
                }
            }
        };
        series.deviceCode = deviceCode;
        series.pointCode = pointCode;
        return series;
    };
    self.addTreeNode = function (treeNode) {
        self.chartQuery.addTreeNode(treeNode);
    };
    self.string2date = function (str) {
        var date = new Date(Date.parse(str.replace(/-/g, "/")));
        return date;
    };
}